package server;

import java.awt.Color;
import java.awt.LayoutManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

import view.PlayGround;

import model.Player;
import model.Segment;

public class Server {
	private ArrayList<Player> connectionArray = new ArrayList<Player>();

	public Server() {

	}

	public void run() {
		final int PORT = 12345;
		final Server s = this;

		try {
			ServerSocket serverSocket = new ServerSocket(PORT);
			int id = 0;
			Color[] colors = { Color.RED, Color.BLUE };
			Segment[][] segs = { { new Segment(15, 15, 10) },
					{ new Segment(30, 15, 10) } };
			while (connectionArray.size() < 2) {
				System.out.println("Waiting for client " + id + "...");
				Socket socket;
				socket = serverSocket.accept();

				System.out.println("Client connected from: "
						+ socket.getInetAddress().getHostName());
				
				PrintWriter output = new PrintWriter(socket.getOutputStream(),
						true);
				
				Scanner input = new Scanner(socket.getInputStream());

				Player tmp = new Player(id, "asd", colors[id], socket, output,
						input);
				
				tmp.addSegment(segs[id]);
				int[] bla = {0,1};
				tmp.setDirection(bla);
				id++;
				connectionArray.add(tmp);
				tmp.send("hallo");
				System.out.println(input.nextLine());
			}
			System.out.println("schleife weg");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
	public void logic() throws InterruptedException {
		while (true) {
			for (Player p : this.connectionArray) {
				p.move();
			}
			
		}
	}

	public void start() {
		final int PORT = 12345;
		final Server s = this;
		Thread waitingThread = new Thread() {

			@Override
			public void run() {
				try {
					ServerSocket serverSocket = new ServerSocket(PORT);
					int id = 0;
					Color[] colors = { Color.RED, Color.BLUE };
					Segment[][] segs = { { new Segment(15, 15, 10) },
							{ new Segment(30, 15, 10) } };
					while (connectionArray.size() < 2) {
						System.out.println("Waiting for client " + id + "...");
						Socket socket;
						socket = serverSocket.accept();
						// connectionArray.add(socket);

						System.out.println("Client connected from: "
								+ socket.getInetAddress().getHostName());
						PrintWriter output = new PrintWriter(
								socket.getOutputStream(), true);
						Scanner input = new Scanner(socket.getInputStream());

						Player tmp = new Player(id, "asd", colors[id], socket,
								output, input);
						tmp.addSegment(segs[id]);
						id++;
						connectionArray.add(tmp);
						tmp.send("hallo");
						System.out.println(input.nextLine());
					}
					System.out.println("schleife weg");
				} catch (IOException e) {
					e.printStackTrace();
				}

			}

		};
		waitingThread.start();
	}

	private ArrayList<Player> getPlayer() {
		return this.connectionArray;
	}

	public static void main(String[] args) throws InterruptedException {
		Server server = new Server();
		server.run();
		PlayGround pg = new PlayGround(server.getPlayer());
		
		server.logic();
		
	}

}
